D40_W4-HW4


Posted by Christy on 2021-05-29

W4 HW4

  • 解題想法:東試西試,第一次寫總是搞不懂要怎麼下手,用 header 來帶資料是有一個方式的,把扣刪掉再重寫一次,下面的程式碼:
const request = require('request')

request({
  url: 'https://api.twitch.tv/kraken/games/top',
  headers: {
  'Accept': 'application/vnd.twitchtv.v5+json',
  'Client-ID': 'kdjf2a;3lsk3sjsdfswl3'
  }
},
  function (err, res, body) {
    if (err) {
      console.log(err)
      return
    }

    console.log(JSON.parse(body))
  }
)
// 印出來以後會發現,這是一個物件,所以重點就是要去裡面拿想要的東西:
// {
//   _total: 3382,
//   top: [
//     { game: [Object], viewers: 476362, channels: 5342 },
//     { game: [Object], viewers: 457325, channels: 3876 },
//     { game: [Object], viewers: 365063, channels: 3115 },
//     { game: [Object], viewers: 272000, channels: 4979 },
//     { game: [Object], viewers: 159120, channels: 79 },
//     { game: [Object], viewers: 150769, channels: 2001 },
//     { game: [Object], viewers: 148224, channels: 3200 },
//     { game: [Object], viewers: 136310, channels: 8326 },
//     { game: [Object], viewers: 98030, channels: 4259 },
//     { game: [Object], viewers: 87250, channels: 1292 }
//   ]
// }
  • 寫成下面的碼就可以拿到觀看人數跟遊戲名稱了
const request = require('request');

request({
  method: 'GET',
  url: `https://api.twitch.tv/kraken/games/top`,
  headers: {
    'Client-ID': 'kdjf2a;3lsk3sjsdfswl3',
    'Accept': 'application/vnd.twitchtv.v5+json'
  }
}, function(err, res, body) {
  if (err) {
    return console.log(err)
  }
  const data = JSON.parse(body)
  const games = data.top
  for (let i = 0; i < games.length; i ++) {
    console.log(games[i].viewers + ' ' + games[i].game.name)
  }
})

W4 HW5

第二題:
410 Gone
在串 Twitch API 的時候,出現了 410 這個錯誤訊息代碼,顯示為 Gone。
意思是說 server 端的內容已經永久地被刪除了,並且沒有轉址,客戶端應該要改變路徑,去別的地方拿資料。

502 Bad Gateway
網頁轉發上游 server 發出的錯誤的回應,不見得是上游的 server 有問題,也有可能是不同意此項資訊交換。
解決辦法是重新連線一次,會出現的情況有可能是在搶購演唱會門票的時候。

第三題:
參考了本週作業的 API 文件,應該最少要有 Base URL,然後要說明各個功能怎麼用。搜尋了一下網路資料,正在嘗試用 Apiary 做一份 API 文件。

我這幾天都忘了在當天發筆記。










Related Posts

React 只會更新畫面中有變化的部分

React 只會更新畫面中有變化的部分

Vim Editor Notes

Vim Editor Notes

Laravel and Cypress integration

Laravel and Cypress integration


Comments